Skip to content

Cache key 51D_ values, per-visitor cache tests, thread-safe LRU cache and a warning for a cloud engine cache - #301

Merged
jwrosewell merged 7 commits into
rcfrom
fix/cloud-cache-rules-and-lru-lock
Dec 1, 2026
Merged

jwrosewell merged 7 commits into
rcfrom
fix/cloud-cache-rules-and-lru-lock

Conversation

@jwrosewell

Copy link
Copy Markdown
Contributor

Behaviour fixes and missing tests found while checking rc (802efb1, after #300) before the next release. One commit each, listed in order. Every commit keeps LF line endings, and the only file whose line endings change is the one restored to LF below.

What changes

  1. Each value made for one visitor is proved to keep a request out of the cache (8f18579, tests only).
    The existing tests supplied the JA4 and HTTP/2 settings together, or the session id and sequence together, so removing one name from Constants.UNCACHED_EVIDENCE_NAMES left every test passing.

    • pipeline/fiftyone_pipeline_cloudrequestengine/tests/test_cloudrequestengine_cache.py lines 325 to 337 test the session id, the sequence, the JA4 and the HTTP/2 settings each alone, under two spellings (another prefix and another letter case), with no other value made for one visitor. Each request reaches the cloud service carrying the value, is not answered from a cache that holds an entry for its evidence, and leaves nothing in the cache.
    • device-detection/fiftyone_devicedetection_cloud/tests/test_builder_cache.py line 115 builds the Device Detection Cloud Pipeline Builder with its defaults, the JavaScript builder included, and a cache. Three identical requests each reach the cloud service and the cache stays empty, because the sequence element gives every request a session id and a sequence.
  2. The Cloud Request Engine's cache key holds every 51D_ query and cookie value (96f6bb6).
    A page's scripts send their answers under a name that starts 51D_ and changes with each script, so the cloud service lists none of them among its evidence keys, and the cache key was made from the listed keys alone. A request carrying answers but no session id, sequence, JA4 or HTTP/2 settings (with add_sequence_element off, or from a caller's own route) was answered from the cache with the response to other answers.

    • cloudrequestengine.py lines 119 to 148 add is_answer_key and CloudEvidenceKeyFilter, which the engine now makes at line 610. The engine accepts, beside the listed keys, every query or cookie value whose name starts 51D_ in any letter case, the bare name 51D_ included, and so these values are part of the cache key. The constants are in constants.py lines 155 to 161.
    • This matches the Rust Cloud Request Engine, whose evidence filter accepts the same names whether or not the service's evidence keys are known (is_answer_key and AcceptedEvidence). In Python the filter only exists once the keys are known, as before.
    • The filter's list still holds the listed keys alone, so the Vary header the web integration writes is unchanged. What the engine sends is unchanged, as it already sent every evidence value. Being accepted evidence, the 51D_ values are now part of the ETag of the web integration's client-side responses.
    • Tests in test_cloudrequestengine_cache.py lines 339 and 359, both failing before the change: two requests that differ only in a 51D_ value each reach the cloud service and get their own answer, and the filter accepts and refuses the same keys as the Rust tests do. test_discovery_at_build.py now watches CloudEvidenceKeyFilter, the class the engine makes.
  3. The server route test checks 51D_ values unchanged on the wire (40f504f, tests only).
    test_server_route_pass_through.py: the posted answer and the cookie answer now carry +, %, =, a space and characters outside ASCII. WireClient (line 80) gives every request to RequestClient, the engine's default client, with the network replaced, and keeps the body it would send. test_session_id_sequence_and_answers_reach_the_cloud (line 217) checks each body is a form body of ASCII text, that reading it as UTF-8 form data gives back each value exactly, once, and that each value is form encoded (+ as %2B, % as %25, = as %3D, a space as +, UTF-8 bytes percent encoded). The test fails if RequestClient joins the body without encoding it, and if the engine decodes a value before sending it.

  4. LRUEngineCache is safe to share between threads (7a018ec).
    pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py lines 44 to 54 hold a threading.Lock around each read and write of the cachetools LRUCache, which reorders on every read and evicts on a write. Without it, concurrent requests raised KeyError and RuntimeError: OrderedDict mutated during iteration from inside the cache. pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py has eight threads hammering one cache, with a cache that records the most threads inside it at once (one) and a run of 24,000 reads and writes over more keys than the cache holds. Both tests failed on three runs out of three without the lock.

  5. test_javascriptbuilder_object_name.py has LF line endings again (d26c2c9).
    The change that kept two blank lines before _declared_names (Start Node once before the object name tests, outside their time limit #296) also turned every line ending to CRLF. With white space ignored, the file now differs from its version before Start Node once before the object name tests, outside their time limit #296 only in setUpModule and the two constants NODE_CALL_SECONDS and NODE_START_SECONDS.

  6. Examples vs Published Packages waits for the version a publish released (b7fda59).
    After a run of Publish or Nightly Pipeline, .github/workflows/examples-published.yml pinned each package to what PyPI's JSON gave as its latest version at that moment, which straight after an upload can still be the version before. ci/publish-package.ps1 pushes the tag <package>/<version> on the published commit once each package is uploaded, so the step "Choose the published versions" (line 160) now reads the tags on workflow_run.head_sha with git ls-remote, pins each package the publish released to its tagged version, and waits until PyPI's simple index (what pip reads) lists it, asking every $PyPIRetrySeconds (15) seconds for at most $PyPIWaitSeconds (900) seconds, then fails naming the package. A package with no tag on that commit was not released, so it takes its latest version as before, and a version given by hand is used as before. Run locally against PyPI: a commit with no tags pinned every package to its latest version, a version given by hand was used throughout, and the 4.6.1 commit pinned the six pipeline packages to 4.6.1 at once and failed (with a short wait) on fiftyone-devicedetection-shared 4.6.1, which PyPI no longer lists.

  7. A cache given to a cloud engine is ignored with a warning, so existing code keeps starting (155cc98).
    Key the cloud engines' cache on the request's evidence and apply the Cloud Request Engine's cache rules #300 made CloudEngine.set_cache raise, which would stop code that gave a cloud engine a cache, and worked before, at start-up after a patch release. cloudengine.py line 104: set_cache on a CloudEngine, and so on DerivedCloud and every engine built on it, now accepts the call and keeps no cache, and logs one warning per engine naming it (class and data key) and saying to add the cache to the Cloud Request Engine. The warning goes to the logger of each pipeline the engine is in, or to the module's logger fiftyone_pipeline_cloudrequestengine.cloudengine while it is in none, and nothing is logged per request. CloudEngine.process still never uses a cache. This matches the Rust cloud engines, where a cache setting is read and has no effect, with a warning logged. The tests from Key the cloud engines' cache on the request's evidence and apply the Cloud Request Engine's cache rules #300 that asserted the raise (test_cloudengine_cache.py, test_human_confidence.py) now assert the call succeeds, the warning is logged once, the cache is never read or written and each request gets its own values. They failed while set_cache raised, and the warning tests fail if the warning is logged on every call. No builder test asserted the raise.

Removing a name from the bypass list

Each new test in item 1 was run with Constants.UNCACHED_EVIDENCE_NAMES edited and then restored.

Removed Failed Passed
session-id test_a_session_id_alone_is_sent_and_never_cached (both spellings) the other three, the builder test
sequence test_a_sequence_alone_is_sent_and_never_cached (both spellings) the other three, the builder test
ja4 test_a_ja4_alone_is_sent_and_never_cached (both spellings) the other three, the builder test
h2 test_http2_settings_alone_are_sent_and_never_cached (both spellings) the other three, the builder test
session-id and sequence the session id and sequence tests, test_with_the_javascript_builder_each_request_reaches_the_cloud the JA4 and HTTP/2 tests
nothing none all

The builder test fails only when both are removed, because the sequence element sets both and either one alone sends the request to the cloud service.

51D_ values whose names collide

Not changed. The Rust package also sends one value in both cases.

  • A 51D_ cookie and a 51D_ query value whose names differ only in letter case: Rust strips the prefix and keeps one value per name in any letter case, the query value winning (build_content), as Python's add_query_data does.
  • A query name that repeats: Rust's evidence keeps one value per key (insert), and its web integration adds the pairs in order, so the last value wins (build_evidence). Python's webevidence reads request.args.items(), which keeps the first value. Both send one value. Which of the repeated values is kept differs, and is left as it is here.

Local test runs

Python 3.12.10 on Windows, each package's tests from its folder with python -m pytest -m "not live and not staging", the packages installed editable from this branch.

Package Passed Subtests passed Deselected (live or staging)
fiftyone_pipeline_core 627 289 0
fiftyone_pipeline_engines 173 53 0
fiftyone_pipeline_engines_fiftyone 156 20 0
fiftyone_pipeline_cloudrequestengine 209 136 4
fiftyone_pipeline_cloudrequestengine examples 116 0 17
fiftyone_devicedetection_cloud 26 9 9
fiftyone_ipintelligence_cloud 16 0 0
fiftyone_location 14 4 3

No test failed. The on-premise packages and the Device Detection and IP Intelligence examples were not run here, as they need the native builds and data files, and the live tests need the CI keys, so CI is their check.

The Examples vs Published Packages check runs on this pull request because it changes that workflow. On a pull request it tests the examples against the packages already on PyPI, which do not have rc's changes yet, so it is expected to fail here as it did on #294 and #298.

…e cache

The Cloud Request Engine sends a request carrying a session id, a sequence,
a JA4 or HTTP/2 settings to the cloud service whatever its cache holds, and
stores no answer to it. The existing tests supplied the JA4 and HTTP/2
settings together, or the session id and sequence together, so removing one
name from Constants.UNCACHED_EVIDENCE_NAMES left every test passing.

- test_cloudrequestengine_cache.py tests each of the four values alone,
  under two spellings each (another prefix and another letter case), with
  no other value made for one visitor. Each request reaches the cloud
  service carrying the value, is not answered from a cache that holds an
  entry for its evidence, and leaves nothing in the cache, so the same
  request again reaches the cloud service too.
- test_builder_cache.py in fiftyone_devicedetection_cloud builds the
  Device Detection Cloud Pipeline Builder with its defaults, the JavaScript
  builder included, and a cache. Three identical requests each reach the
  cloud service, because the sequence element gives every request a
  session id and a sequence, and the cache stays empty.

Each new test fails when its name is removed from the list. The builder
test fails when both the session id and the sequence are removed, as
either one alone still sends the request to the cloud service.
…alue

A page's scripts send their answers under a name that starts 51D_ and
changes with each script, so the cloud service lists none of them among its
evidence keys. The engine's cache key was made from the listed keys alone,
so a request carrying answers but no session id, sequence, JA4 or HTTP/2
settings (with add_sequence_element off, or from a caller's own route) was
answered from the cache with the response to other answers, and its own
answers never reached the cloud service.

The engine now accepts, beside the keys the service lists, every query or
cookie value whose name starts 51D_ in any letter case, the bare name 51D_
included, as the Rust Cloud Request Engine does. CloudEvidenceKeyFilter
adds the answers to the listed keys, and its list still holds the listed
keys alone, because the answers have no names to list, so the Vary header
the web integration writes is unchanged. What the engine sends is unchanged
too, as it already sent every evidence value. Being accepted evidence, the
answers are now part of the ETag of the web integration's client-side
responses, which changes with them.

Tests in test_cloudrequestengine_cache.py, each failing before this change:
- Two requests that differ only in a 51D_ value, under four spellings,
  each reach the cloud service and get their own answer, and the same value
  again is answered from the cache.
- The filter accepts the 51D_ query and cookie names and refuses a header,
  a server value, a name that only contains 51D_, a key with no prefix and
  a name whose first character is not ASCII.

test_discovery_at_build.py now watches CloudEvidenceKeyFilter, the class
the engine makes, to show a second request makes no filter.
The server route test checked the dictionary MockRequestClient records, and
its 51D_ values held no character a form body encodes, so it could not show
the values reach the cloud service unchanged.

- The posted answer and the cookie answer now carry +, %, =, a space and
  characters outside ASCII, through the Flask application and the web
  integration as before.
- WireClient answers as MockRequestClient does and also gives every request
  to RequestClient, the client the engine uses by default, with the network
  replaced, keeping the URL, the content type and the body it would send.
- The test checks each data request's body is a form body of ASCII text,
  that reading it as the cloud service does (UTF-8) gives back each answer,
  the cookie answer and the session id exactly, once each, and that each
  value is form encoded, with + as %2B, % as %25, = as %3D, a space as +
  and each byte of the UTF-8 of a character outside ASCII percent encoded.

The test fails if RequestClient joins the body without encoding it, and if
the engine decodes a value before sending it.
One LRUEngineCache is shared by every request an engine processes, and a
web server processes requests on several threads at once. The cachetools
LRUCache underneath changes its order on every read and removes the least
recently used entry on a write when full, with nothing holding threads
apart, so concurrent requests could raise KeyError or "OrderedDict mutated
during iteration" from inside the cache, or leave it inconsistent.

get_cache_value and set_cache_value now hold a threading.Lock while they
read or write the LRUCache, so one thread at a time is inside it.

test_lru_cache.py in fiftyone_pipeline_engines, both failing without the
lock on three runs out of three:
- Eight threads read and write one cache whose LRUCache pauses in each
  call and records how many threads are inside it at once, which is one.
- Eight threads make 3,000 reads and writes each on one cache of 8 entries
  over 32 keys, with the interpreter switching threads every microsecond.
  No thread raises, every read gives the value written for its key or
  nothing, the cache holds no more than 8 entries, and it still evicts the
  least recently used entry afterwards.
test_javascriptbuilder_object_name.py had LF line endings until the change
that kept two blank lines before _declared_names, which also turned every
line ending to CRLF, so that change showed as all 350 lines rewritten. The
file has LF line endings again, as every other Python file here does.

With white space ignored, the file differs from its version before the
Node start-up change only in setUpModule and the two constants,
NODE_CALL_SECONDS and NODE_START_SECONDS. Against the first commit of that
change, the only difference is the blank line the second one added.
…mples

After a run of Publish or Nightly Pipeline, the Examples vs Published
Packages workflow pinned each package to what PyPI's JSON gave as its
latest version at that moment, which straight after an upload can still be
the version before, so the examples could be tested against the previous
release with nothing to say so beyond the summary table.

ci/publish-package.ps1 pushes the tag '<package>/<version>' on the
published commit once each package is uploaded. The workflow now reads the
tags on that commit (git ls-remote, as the repository is public) and pins
each package the publish released to the version its tag names, waiting
until PyPI's simple index, which pip reads, lists that version. It asks
every $PyPIRetrySeconds (15) seconds and fails the run, naming the package
and version, after $PyPIWaitSeconds (900) seconds. A package with no tag on
that commit was not released by the publish, so it is pinned to its latest
version as before, and a version given by hand is used as before.

Run locally against PyPI with the step's script: a commit with no tags
pinned every package to its latest version, a version given by hand was
used for every package, and the 4.6.1 commit pinned the six pipeline
packages to 4.6.1 at once and failed, with a short wait, on
fiftyone-devicedetection-shared 4.6.1, which PyPI no longer lists.
… raise

CloudEngine.set_cache raised an exception, so code that gave a cloud engine
a cache, which worked before, would stop at start-up after a patch release.
A cloud engine accepts no evidence, so there is no key to use in a cache of
its own, and the cache belongs on the Cloud Request Engine, where the
pipeline builders now put it.

set_cache on a CloudEngine, and so on DerivedCloud and every engine built
on CloudEngine, now accepts the call and keeps no cache. It logs one
warning for each engine, however often the engine is given a cache, naming
the engine by its class and data key and saying to add the cache to the
Cloud Request Engine. The warning goes to the logger of each pipeline the
engine is in, or to the module's own logger
(fiftyone_pipeline_cloudrequestengine.cloudengine) while it is in none, and
nothing is logged as requests are processed. CloudEngine.process still
never uses a cache. This is how the Rust cloud engines treat a cache
setting, which is read and has no effect, with a warning logged.

Tests, the first three failing while set_cache raised:
- test_cloudengine_cache.py: a cache given before the engine is in a
  pipeline is accepted with one warning on the module's logger, and one
  given once the engine is in a pipeline with one warning on the
  pipeline's logger. In both, the engine keeps no cache, three requests
  each get their own value, the cache is never read or written and nothing
  more is logged. A cache assigned to the engine directly is never read or
  written either.
- test_human_confidence.py: DerivedCloud accepts a cache with one warning
  naming it, and a cache given through set_cache or assigned directly never
  serves a redeemed result and is never read or written.

Each warning test fails if the warning is logged on every call.
@jwrosewell
jwrosewell merged commit c74459c into rc Dec 1, 2026
9 of 10 checks passed
@jwrosewell
jwrosewell deleted the fix/cloud-cache-rules-and-lru-lock branch December 1, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant